﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Route_Theme_Entity : EntityBase<ROUTE_THEME>
    {
        public override RESP_Info Add(ROUTE_THEME model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into ROUTE_THEME(ROUTE_ID,THEME_ID) values (@ROUTE_ID,@THEME_ID)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", model.ROUTE_ID));
                    cmd.Parameters.Add(new SqlParameter("THEME_ID", model.THEME_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加旅程主题成功</p></h5>";
                    info.Url = "RouteThemeList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加旅程主题失败</p></h5>";
                    info.Url = "RouteThemeAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加旅程主题失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(ROUTE_THEME model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update ROUTE_THEME set THEME_ID=@THEME_ID where ROUTE_ID=@ROUTE_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", model.ROUTE_ID));
                    cmd.Parameters.Add(new SqlParameter("THEME_ID", model.THEME_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑旅程主题成功</p></h5>";
                    info.Url = "RouteThemeList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑旅程主题失败</p></h5>";
                    info.Url = "RouteThemeAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑旅程主题失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(ROUTE_THEME routeTheme)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from ROUTE_THEME where ROUTE_ID=@ROUTE_ID AND THEME_ID=@THEME_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", routeTheme.ROUTE_ID));
                    cmd.Parameters.Add(new SqlParameter("THEME_ID", routeTheme.THEME_ID));

                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除旅程主题成功</p></h5>";
                    info.Url = "RouteThemeList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除旅程主题失败</p></h5>";
                    info.Url = "RouteThemeList.aspx";

                    Tom.Net.Log.LogManager.Error("删除旅程主题失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override ROUTE_THEME GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select ROUTE_ID,THEME_ID from ROUTE_THEME where ROUTE_ID=@ROUTE_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        ROUTE_THEME routeTheme = new ROUTE_THEME();
                        if (!reader.IsDBNull(0)) routeTheme.ROUTE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routeTheme.THEME_ID = reader.GetString(1);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return routeTheme;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取旅程主题数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override ROUTE_THEME GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        ROUTE_THEME routeTheme = new ROUTE_THEME();
                        if (!reader.IsDBNull(0)) routeTheme.ROUTE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routeTheme.THEME_ID = reader.GetString(1);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return routeTheme;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取旅程主题数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<ROUTE_THEME> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<ROUTE_THEME> routeThemes = new List<ROUTE_THEME>();
                    while (reader.Read())
                    {
                        ROUTE_THEME routeTheme = new ROUTE_THEME();
                        if (!reader.IsDBNull(0)) routeTheme.ROUTE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routeTheme.THEME_ID = reader.GetString(1);

                        routeThemes.Add(routeTheme);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return routeThemes;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取旅程主题数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}